package chenji.gunapi.pluginmessage;

import java.util.HashSet;

import org.bukkit.entity.Player;

import chenji.gunapi.GunAPI;
import chenji.gunapi.manager.Manager;
import chenji.gunapi.pluginmessage.packages.OutVerifyPackage;
import chenji.gunapi.task.Taskable;

public class PlayerClientVerify extends Manager implements Taskable {
	private HashSet<PlayerWaiter> waiter = new HashSet<>();

	public PlayerClientVerify() {
		GunAPI.getGunAPI().getGunTaskManager().registerPerSecondTask(this);
		this.load();
	}

	@Override
	public void onPlayerQuit(Player p) {
		PlayerWaiter pw = null;
		for (PlayerWaiter pw1 : this.waiter) {
			if (pw1.equals(p)) {
				pw = pw1;
				break;
			}
		}
		if (pw == null) {
			return;
		}
		this.waiter.remove(pw);
	}

	@Override
	public void onDisable() {
		this.waiter.clear();
	}

	@Override
	public void onPlayerJoin(Player p) {
		
		PlayerWaiter waiter = new PlayerWaiter(p);
		this.waiter.add(waiter);
	}

	@Override
	public void task() {
		
		for (PlayerWaiter pw1 : this.waiter) {
			pw1.task();
		}
	}
	public void receiveVerfiy(Player arg1) {
		PlayerWaiter pw = null;
		for (PlayerWaiter pw1 : this.waiter) {
			if (pw1.equals(arg1)) {
				pw = pw1;
				break;
			}
		}
		if (pw == null) {
			return;
		}
		this.waiter.remove(pw);
		
	}
}

class PlayerWaiter {
	private Player p;
	private int time;

	public PlayerWaiter(Player p) {
		this.p = p;
		this.time = 0;
	}

	public void task() {
		this.time++;
		if(time==5){
			PluginMessageUntl.getPluginMessageUntl().getPackageManager().sendPackage(p, new OutVerifyPackage());
		}
		if (this.time >= 20) {
			if (GunAPI.getGunAPI().kickwithourmod()) {
				p.kickPlayer("§c请安装GunAPI客户端的Mod");
			}
		}
	}

	public boolean equals(Object o) {
		return this.p.equals(o);
	}
}